<?php

class Brand extends AppModel {

    var $name = 'Brand';
    var $displayField = 'brand_description';
    var $validate = array(
        'brand_description' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Name cannot be left empty'
            )
        )
    );
    var $hasMany = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'global_brands',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'ProductStyle' => array(
            'className' => 'Product',
            'foreignKey' => 'global_styles',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'ProductCondition' => array(
            'className' => 'Product',
            'foreignKey' => 'global_conditions',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Categorybrand' => array(
            'className' => 'Categorybrand',
            'foreignKey' => 'brand_id',
            'conditions' => '',
            'fields' => '',
            'dependent' => true
        ),
        'BrandPermission' => array(
            'className' => 'BrandPermission',
            'foreignKey' => 'brand_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    function globalBrands($id, $id2, $c='') {
        
        if ($id2 == "") {
            $id2 = "A";
            $c = "A";
        }
        if(is_numeric($id2)){
            $id2="A";
            $c="A";            
        }
        
        $condition = 'Brand.brand_description LIKE';
        $value = $id2 . '%';
        if ($id2 == "123") {
            $condition = 'Brand.brand_description REGEXP';
            $value = '[0-9]';
        }
        $content = $this->find('all', array('conditions' => array('Brand.brand_type' => $id, 'Brand.global' => 'Yes', $condition => "$value"), 'order' => 'Brand.id DESC'));
        if (empty($content) && $c == "A") {
            $content = $this->globalBrands($id, ++$id2, $c);
        }
        return $content;
    }

}

?>